//
//  HDCFForgetPwVc.m
//  HaoDeCaiFu
//
//  Created by 郑开宏 on 2017/11/15.
//  Copyright © 2017年 郑开宏. All rights reserved.
//

#import "HDCFForgetPwVc.h"
#import "JKCountDownButton.h"
#import "SVProgressHUD.h"

@interface HDCFForgetPwVc ()
@property (weak, nonatomic) IBOutlet UITextField *pwOne;
@property (weak, nonatomic) IBOutlet UITextField *pwTwo;
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;
@property (weak, nonatomic) IBOutlet JKCountDownButton *sendCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;

@property (nonatomic, copy) NSString *token;

@end

@implementation HDCFForgetPwVc

- (void)setIsTradeForget:(BOOL)isTradeForget{
    _isTradeForget = isTradeForget;
}

- (IBAction)sendCodeClick:(JKCountDownButton *)sender {
    if (_phoneTF.text.length < 1) {
        [SVProgressHUD showInfoWithStatus:@"请输入证件姓名"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    if (_phoneTF.text.length < 1) {
        [SVProgressHUD showInfoWithStatus:@"请输入证件号码"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    [sender startWithSecond:60];
    [self getCode];
}
- (IBAction)confirmBtnClick:(UIButton *)sender {
    if (_pwOne.text.length < 1) {
        [SVProgressHUD showInfoWithStatus:@"请输入证件姓名"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    if (_pwTwo.text.length < 1) {
        [SVProgressHUD showInfoWithStatus:@"请确认证件号码"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    if (_codeTF.text.length < 1) {
        [SVProgressHUD showInfoWithStatus:@"请输入验证码"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    if (_phoneTF.text.length < 1) {
        [SVProgressHUD showInfoWithStatus:@"请输入新密码"];
        [SVProgressHUD dismissWithDelay:1.0];
        return;
    }
    [self yanzhengOtp];
}

- (void)yanzhengOtp{
    [SVProgressHUD show];
    NSMutableDictionary *datadict = [NSMutableDictionary dictionary];
    datadict[@"accountNumber"] = kAcount;
    datadict[@"opt"] = self.codeTF.text;
    
    NSString *jsonString = [self dictionaryToJson:datadict];
    
    NSDictionary *paramet = @{KparametOne:jsonString,
                              KparametTwo:@"/public/investor/find-bank"
                              };
    [HttpRequest sendPostRequestWithURL:nil WithParamets:paramet WithJsonString:nil success:^(id result) {
        if ([result[@"code"] isEqualToString:@"0000"]) {
            self.token = result[@"data"];
            [self saveNewPw];
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"message"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
        
    } failure:^(NSError *errer) {
        [SVProgressHUD showInfoWithStatus:@"服务器错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}

- (void)getCode{
    [SVProgressHUD show];
    NSMutableDictionary *datadict = [NSMutableDictionary dictionary];
    datadict[@"idNumber"] = self.pwTwo;
    datadict[@"idName"] = self.pwOne;
    
    NSString *jsonString = [self dictionaryToJson:datadict];
    
    NSDictionary *paramet = @{KparametOne:jsonString,
                              KparametTwo:@"/public/investor/find-bank"
                              };
    [HttpRequest sendPostRequestWithURL:nil WithParamets:paramet WithJsonString:nil success:^(id result) {
        [SVProgressHUD showInfoWithStatus:@"发送成功"];
        [SVProgressHUD dismissWithDelay:1.0];
    } failure:^(NSError *errer) {
       [SVProgressHUD showInfoWithStatus:@"服务器错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}

- (void)saveNewPw{
    NSMutableDictionary *datadict = [NSMutableDictionary dictionary];
    datadict[@"token"] = self.token;
    datadict[@"newPassword"] = self.phoneTF.text;
    NSString *jsonString = [self dictionaryToJson:datadict];
    NSDictionary *paramet = @{KparametOne:jsonString,
                              KparametTwo:@"/public/investor/find-bank"
                              };
    [HttpRequest sendPostRequestWithURL:nil WithParamets:paramet WithJsonString:nil success:^(id result) {
        if ([result[@"code"] isEqualToString:@"0000"]) {
            self.token = result[@"data"];
            [SVProgressHUD showSuccessWithStatus:@"重设成功"];
            [SVProgressHUD dismissWithDelay:1.0];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        }else{
            [SVProgressHUD showInfoWithStatus:result[@"message"]];
            [SVProgressHUD dismissWithDelay:1.0];
        }
    } failure:^(NSError *errer) {
        [SVProgressHUD showInfoWithStatus:@"服务器错误"];
        [SVProgressHUD dismissWithDelay:1.0];
    }];
}

- (NSString *)dictionaryToJson:(NSDictionary *)dic{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSString *jsonTemp = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
    return jsonTemp;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBezierPath *maskPath=[UIBezierPath bezierPathWithRoundedRect:self.sendCodeBtn.bounds byRoundingCorners:UIRectCornerTopRight|UIRectCornerBottomRight cornerRadii:CGSizeMake(6, 6)];
    CAShapeLayer *maskLayer=[[CAShapeLayer alloc]init];
    maskLayer.frame=self.sendCodeBtn.bounds;
    maskLayer.path=maskPath.CGPath;
    self.sendCodeBtn.layer.mask=maskLayer;
    self.sendCodeBtn.clipsToBounds = YES;
    
    [self.navigationItem setTitle:@"重设密码"];
}



@end
